***************** Replication of Franchino, Fabio, and Camilla Mariotto. “Bargaining Outcomes and Success in EU Economic Governance Reforms”. Political Science Research and Methods.

* Step 1 to reproduce Figure 2

version 16

******************************* SENSITIVITY ANALYSIS  - Add Gaussian random noise to positions
*************   COMPUTING POINT PREDICTIONS: Minimax model
clear all
set obs 1
gen v = _n/10
save mad_minimax_noise.dta, replace

set seed 1234

local m =  1  /* iteration per each s.d. value */
while `m' <= 1000 {

local v = 0.1  /* v is s.d of Gaussian noise - s.d. of country and institutional positions is 41.74882 */
while `v' <= 42.1 {
* Upload original dataset
use NBSmodel_data.dta, clear
gen v = `v'
gen minimax = 0
gen disutility_max = .
order disutility_max minimax

* Add Gaussian noise N(0,v) to Commission positions
generate random = rnormal(0,`v')
replace comm=comm+random
replace comm=100 if comm>100 
replace comm=0   if comm<0 

* Add Gaussian noise N(0,v) to Parliament positions
replace random = rnormal(0,`v')
replace ep_1=ep_1+random
replace ep_1=100 if ep_1>100 
replace ep_1=0   if ep_1<0 

* Add Gaussian noise N(0,v) to country positions
local j = 1
while `j' <= 27 {
replace random = rnormal(0,`v')
replace country`j'=country`j'+random if country`j'!=.
replace country`j'=100 if country`j'>100 
replace country`j'=0   if country`j'<0 
local j = `j' + 1
}

* Gradient ascent procedure
local p = 0
while `p' <= 100 {
gen minimax_p = `p'
gen disutility_p=0
order minimax* disutility*
* compute maximum disutility of actors for p=`p'
local c = 1
while `c' <= 27 {
gen utility`c' =	salience`c'	*(`p' -	country`c')^2 if	salience`c'	> 0 &	salience`c'	!= .
replace disutility_p = utility`c' if utility`c' > disutility_p & utility`c' != .
local c = `c' + 1
}
gen utility_comm =	salience_comm	*(`p' -	comm	)^2 if	salience_comm	> 0 &	salience_comm	!= .
replace disutility_p = utility_comm if utility_comm > disutility_p
gen utility_ep =	salience_ep	*(`p' -	ep_1)^2 if	salience_ep	> 0 &	salience_ep	!= .
replace disutility_p = utility_ep if utility_ep > disutility_p & ep_rule==2

* replace minimax and disutility
replace minimax = minimax_p if `p'==0
replace disutility_max = disutility_p if `p'==0

replace minimax = minimax_p if  disutility_p < disutility_max
replace disutility_max = disutility_p if  disutility_p < disutility_max
order minimax* disutility* utility_comm utility_ep utility*
drop minimax_p* disutility_p* utility*
local p = `p' + 1
}

order disutility_max minimax sq outcome
sort act issue_id
label variable minimax "mimax solution"
label variable disutility_max "maximum utility loss" 

* Compute means of absolute differences between outcomes and model predictions
gen mad`m'=abs(minimax-outcome)
collapse v mad`m'
label variable mad`m' "mean of absolute differences" 
label variable v "s.d of Gaussian noise" 

merge 1:1 v using mad_minimax_noise.dta, nogen
save mad_minimax_noise.dta, replace
sort v
pause 1000

       
local v = `v' + 1 /* increments of s.d. */
}

local m = `m' + 1 /* simulations */
}

egen minimax = rowmean(mad*)
label variable minimax "mean of absolute differences across m simulations" 
egen minimax_u = rowpctile(mad*), p(97.5)
label variable minimax_u "upper boundary" 
egen minimax_l = rowpctile(mad*), p(2.5)
label variable minimax_l "lower boundary" 

drop mad*
save mad_minimax_noise.dta, replace

exit

